package com.sinosoft.brokerchannel.base.utile;

import org.springframework.cglib.beans.BeanCopier;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 需求名称：[大童理赔渠道平台]<br/>
 * 类描述:[复制对象]<br/>
 *
 * @author shilk
 * @version V1.0
 * @date 2017-12-15 14:48
 */

public class BeanCopierUtils {
    public static Map<String,BeanCopier> beanCopierMap = new ConcurrentHashMap<>();

    public static void copyProperties(Object target, Object source){
        String beanKey =  generateKey(source.getClass(), target.getClass());
        BeanCopier copier;
        if(!beanCopierMap.containsKey(beanKey)) {
            copier = BeanCopier.create(source.getClass(), target.getClass(), true);
            beanCopierMap.put(beanKey, copier);
        } else {
            copier = beanCopierMap.get(beanKey);
        }
        copier.copy(source, target, new ConverterBeanCopierForCopy());
    }

    private static String generateKey(Class<?> class1, Class<?>class2){
        return class1.toString() + class2.toString();
    }
}
